package cn.signalpha.modules.shop.controller.seller;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.DateUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.com.entity.User;
import cn.signalpha.modules.com.service.UserService;
import cn.signalpha.modules.shop.dto.CouponDTO;
import cn.signalpha.modules.shop.entity.Coupon;
import cn.signalpha.modules.shop.service.CouponService;
import cn.signalpha.modules.shop.service.CouponUserService;
import cn.signalpha.modules.shop.vo.CouponUserVO;
import cn.signalpha.modules.shop.vo.CouponVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/sys-seller/coupon")
@Api(value = "优惠券", tags = "优惠券")
public class SpiCouponController extends BaseController {

	private final UserService userService;
	private final CouponService couponService;
	private final CouponUserService couponUserService;

    @GetMapping("/page")
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<CouponVO>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("sellerId", getUser().getTenantId());
        return R.data(couponService.selectCouponPage(Condition.getPage(query), params));
    }

	@GetMapping("/user/page")
	@ApiOperation(value = "领取记录", notes = "")
	public R<IPage<CouponUserVO>> userPage(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		return R.data(couponUserService.selectCouponUserPage(Condition.getPage(query), params));
	}

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<Coupon> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(couponService.getById(id));
	}

	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "传入coupon")
	public R save(@Valid @RequestBody Coupon coupon) {
    	Date date = DateUtil.now();
		coupon.setSellerId(Long.valueOf(getUser().getTenantId()));
		coupon.setCreateTime(date);
		coupon.setUpdateTime(date);
		coupon.setLastTotal(coupon.getTotal());
		return R.status(couponService.save(coupon));
	}

	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "传入coupon")
	public R update(@Valid @RequestBody Coupon coupon) {
		coupon.setSellerId(Long.valueOf(getUser().getTenantId()));
		coupon.setUpdateTime(DateUtil.now());
		return R.status(couponService.updateById(coupon));
	}

	@PostMapping("/send")
	@ApiOperation(value = "发放", notes = "传入coupon")
	public R send(@Valid @RequestBody CouponDTO.SendDTO sendDTO) {
		Func.toLongList(sendDTO.getIds()).forEach(id -> {
			List<User> userList = userService.listByIds(Func.toLongList(sendDTO.getUserIds()));
			userList.forEach(user -> couponService.receiveCoupon(id, user.getId(), 2));
		});
		return R.status(true);
	}

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(couponService.removeByIds(Func.toLongList(ids)));
	}

}
